package cn.agiledata.bank.barcodepayment.logic;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;

import cn.agiledata.bank.barcodepayment.exception.BarCodePayException;
import cn.agiledata.bank.common.util.DateUtil;
/**
 * 涓婃捣缃戠粶閫氫俊鍏徃鏀惰垂鏉″舰鐮�
 * 
 * @author 马贤波
 * 
 */
public class CncBarCode extends AbstractBarCode {
    /**
     * 鏉″舰鐮佸�鐨勯暱搴�
     */
    private static final int CODE_SIZE = 34;
    
    public BigDecimal getAmount() {
        return getAmount(23, 32);
    }

    public Date getDate() {
        String ds = getValue().substring(16,20);
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        String dateValue = (year / 100) + ds;
        return DateUtil.string2Date(dateValue, "yyyyMM");
    }
    protected void validate(String barCodeValue) throws BarCodePayException {
        super.validate(barCodeValue);
        if (barCodeValue.length() != CODE_SIZE) {
            throw BarCodePayException.ERROR_BARCODE_LENGTH;
        }
        String checkStr = checkCode(barCodeValue);
        int checkNum = Integer.valueOf(checkStr).intValue();
        String end2 = barCodeValue.substring(barCodeValue.length()-2, barCodeValue.length());
        int endNum = Integer.valueOf(end2).intValue();
        if (checkNum != endNum) {
            throw BarCodePayException.ERROR_BARCODE;
        }
        Date d = getDate();
        Calendar codeDate = Calendar.getInstance();
        codeDate.setTime(d);
        int codeYear = codeDate.get(Calendar.YEAR);
        Calendar now = Calendar.getInstance();
        int nowYear = now.get(Calendar.YEAR);
        if (codeYear > nowYear && codeYear < nowYear-1) {
            throw BarCodePayException.ERROR_BARCODE;
        }
    }
}
